package com.serviceprovider.business.module.admin.transaction.utility;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.transaction.model.NewCsvReplica;
import com.serviceprovider.business.module.admin.transaction.valueobjects.TransactionCSV;
import com.serviceprovider.business.module.admin.transaction.valueobjects.TransactionCSVError;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

/**
 * The Class UnsuccessfulDataTag.
 */
public class UnsuccessfulDataTag implements Tag {
    List<TransactionCSVError> errorList = null;

    /** The parent. */
    private Tag parent = null;

    /** The pc. */
    private PageContext pc = null;

    /*
     * (non-Javadoc)
     *
     * @see
     * javax.servlet.jsp.tagext.Tag#setPageContext(javax.servlet.jsp.PageContext
     * )
     */
    public void setPageContext(PageContext p) {
        this.pc = p;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#setParent(javax.servlet.jsp.tagext.Tag)
     */
    public void setParent(Tag t) {
        this.parent = t;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#getParent()
     */
    public Tag getParent() {
        return this.parent;
    }

    /**
     * Gets the error list.
     *
     * @return the errorList
     */
    public List<TransactionCSVError> getErrorList() {
        return this.errorList;
    }

    /**
     * Sets the error list.
     *
     * @param errorList
     *            the errorList to set
     */
    public void setErrorList(List<TransactionCSVError> errorList) {
        this.errorList = errorList;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
     */
    public int doStartTag() throws JspException {
        try {
            if (this.errorList != null) {
                this.pc.getOut().write(createErrorTable(this.errorList));
            }
        } catch (IOException e) {
            throw new JspTagException("An IOException occurred.");
        }

        return SKIP_BODY;
    }

    private String createErrorTable(List<TransactionCSVError> errorDataList) {
        StringBuilder builder = new StringBuilder();

        builder.append("<table width=\"100%\" class=\"formTable\">");
        builder.append("<tbody>");
        builder.append("<tr class=\"formHeader\">");
        builder.append("<td>Trace Number</td>");
        builder.append("<td>Receiver Account</td>");
        builder.append("<td>Receiver Name</td>");
        builder.append("<td>Originator Name</td>");
        builder.append("<td>Transaction Amount</td>");
        builder.append("<td>Originator Code</td>");
        builder.append("<td>Action</td>");
        builder.append("</tr>");

        for (TransactionCSVError transactionCSVError : errorDataList) {
            createTR(builder, transactionCSVError);
        }

        builder.append("</tbody>");
        builder.append("</table>");

        return builder.toString();
    }

    private void createTR(StringBuilder builder, TransactionCSVError transactionCSVError) {
        builder.append("<tr>");
        createTD(builder, transactionCSVError);
        builder.append("</tr>");
    }

    private void createTD(StringBuilder builder, TransactionCSVError transactionCSVError) {
        final NewCsvReplica transactionCSV = transactionCSVError.getTransaction();
        final List<String>  fields         = transactionCSVError.getFields();

        createRecord(builder, (transactionCSV.getTrace_number() == null)
                              ? ""
                              : transactionCSV.getTrace_number(), fields.contains("traceNumber"));
        createRecord(builder, (transactionCSV.getService_provider_id() == null)
                              ? ""
                              : transactionCSV.getService_provider_id(), fields.contains("receiverAccount"));
        createRecord(builder, (transactionCSV.getReceiver_name() == null)
                              ? ""
                              : transactionCSV.getReceiver_name(), fields.contains("receiverName"));
        createRecord(builder, (transactionCSV.getOriginator_name() == null)
                              ? ""
                              : transactionCSV.getOriginator_name(), fields.contains("originatorName"));
        createRecord(builder, (transactionCSV.getTransaction_amount() == null)
                              ? ""
                              : transactionCSV.getTransaction_amount(), fields.contains("credit"));
        createRecord(builder, (transactionCSV.getOrginator_code() == null)
                              ? ""
                              : transactionCSV.getOrginator_code(), fields.contains("originatorCode"));

        if (!transactionCSVError.isDuplicated()) {
            String contextPath = ((HttpServletRequest) this.pc.getRequest()).getContextPath();
            String url         = contextPath.concat("/admin/transaction/edit_transactioncsv_record.htm?record_id=").concat(
                             transactionCSV.getTrace_number());
            String editImageUrl = "<img src=\"".concat(contextPath) + "/images/edit_entry.png\" alt=\"Edit\" />";

            createRecord(builder, editImageUrl, !fields.isEmpty(), url);
        }
    }

    private void createRecord(StringBuilder builder, String string, boolean errorField) {
        if (errorField) {
            builder.append("<td bgcolor=\"red\">");
        } else {
            builder.append("<td>");
        }

        builder.append(string);

        if (errorField) {
            builder.append("</td>");
        } else {
            builder.append("</td>");
        }
    }

    private void createRecord(StringBuilder builder, String string, boolean errorrecord, String url) {
        builder.append("<td>");

        if (errorrecord) {
            builder.append("<a href=\"");
            builder.append(url);
            builder.append("\">");
            builder.append(string);
            builder.append("</a>");
        } else {
            builder.append(string);
        }

        builder.append("</td>");
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
     */
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#release()
     */
    public void release() {
        this.pc        = null;
        this.parent    = null;
        this.errorList = null;
    }
}



